package com.china.book.config;

import org.springframework.boot.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.io.*;
import java.util.stream.Collectors;

/**
 * @description: TODO
 * @author: 唐晓军
 * @date: 2021/8/5 14:33
 * @version: 1.0.0
 * @company: 北京凤凰学易科技有限公司
 **/
@Configuration
public class JwtTokenConfig {

    @Bean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * JwtAccessTokenConverter：TokenEnhancer的子类,帮助程序在JWT编码的令牌值和OAuth身份验证信息之间进行转换(在两个方向上)
     * 同时充当TokenEnhancer授予令牌的时间。
     * 自定义的JwtAccessTokenConverter：把自己设置的jwt签名加入accessTokenConverter中(这里设置'demo',项目可将此在配置文件设置)
     *
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey(readRsaKeyFile("ca/private.key"));
        accessTokenConverter.setVerifierKey(readRsaKeyFile("ca/private.key"));
        return accessTokenConverter;
    }

    /**
     * 自定义JWTokenEnhancer实现TokenEnhancer并重写enhance方法,将附加信息加入oAuth2AccessToken中
     *
     * @return
     */
    @Bean
    public TokenEnhancer jwtTokenEnhancer() {
        return new JwtTokenEnhancer();
    }

    /**
     * 获取本地文件
     *
     * @param file
     * @return
     */
    private static String readRsaKeyFile(String file) {
        ApplicationHome path = new ApplicationHome(JwtTokenConfig.class);
        File filePath = path.getSource();
        String projectPath = filePath.getParentFile().toString();
        if (filePath.getParentFile().getName().equals("target")) {
            projectPath = filePath.getParentFile().getParentFile().toString();
        }
        String filename = projectPath + File.separator + file;
        if (filename == null) {
            return null;
        } else {
            String rsaKey = null;
            try {
                FileInputStream inputStream = new FileInputStream(filename);
                byte[] keyBytes = new byte[inputStream.available()];
                inputStream.read(keyBytes);
                inputStream.close();
                rsaKey = new String(keyBytes, "utf-8");
                return rsaKey;
            } catch (Exception var4) {
                return null;
            }

//            if (rsaKey == null) {
//                return null;
//            } else {
//                String trimmedPrivateKey = rsaKey.replaceAll("(-+BEGIN\\s+(RSA\\s*)?((PRIVATE)|(PUBLIC))\\s+KEY-+\\r?\\n|-+END\\s+(RSA\\s*)?((PRIVATE)|(PUBLIC))\\s+KEY-+\\r?\\n?|\\r?\\n?)", "");
//                return trimmedPrivateKey;
//            }
        }
    }
}
